
const http = require('http')
const fs = require('fs')
const path = require('path')
let server = http.createServer((req,res)=>{
    res.setHeader('Content-Type','text/html;charset=utf8')
    if(req.url === '/'){
        res.write('主页')
        res.end()
    }else if(req.url ==='/login.html'){
        fs.readFile(path.join(__dirname,'./login.html'),'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.write(data)
            res.end()
        })
    }else if(req.url === '/css/404.css'){
        res.setHeader('Content-Type','text/css')
        fs.readFile(path.join(__dirname,'./css/404.css'),(err,data)=>{
            if(err){
                throw err
            }
            res.write(data)
            res.end()
        })
    }
    else if(req.url==='/img/not_found.png'){
        res.setHeader('Content-Type','image/png')
        fs.readFile(path.join(__dirname,'./img/not_found.png'),(err,data)=>{
            if(err){
                throw err
            }
            res.write(data)
            res.end()
        })
    }
    else{
        // res.write('404,页面没有了')
        // res.setHeader('Content-Type','image/png')
        fs.readFile(path.join(__dirname,'./404.html'),'utf8',(err,data)=>{
            if(err){
                throw err
            }
            res.write(data)
            res.end()
        })
       
    }

})

server.listen(3000,()=>{
    let port = server.address().port
    console.log(`server is running... port is ${port}`);
})







































